package com.example.myblog.controller;

import com.example.myblog.entity.Columnist;
import com.example.myblog.service.BlogService;
import com.example.myblog.service.TagService;
import com.example.myblog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;

@Controller
public class TypeShowController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@PathVariable Integer id,Integer page, Model model){
        page = page==null? 0:page;
        List<Columnist> types = typeService.listTopNum(100);
        if (id == -1) {
            id = types.get(0).getId();
        }
        Columnist type = typeService.findById(id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("columnist",type.getColName());
        model.addAttribute("page",blogService.listByCondition(map,page));
        model.addAttribute("types",types);
        model.addAttribute("activeTypeId",id);
        return "types";
    }
}
